SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
            
CREATE procEDURE [dbo].[SP_TWCLAS_CNTA_Q02]                    
/*---------------------------------------------------------------------*/                    
/*----- Empresa          : OFISIS S.A.                            -----*/                    
/*----- Cliente          : OFISIS S.A.                             ----*/                    
/*----- Sistema          : Contabilidad                           -----*/                    
/*----- Modulo           : Empresa                                -----*/                    
/*----- Programa         : Libro Diario Simplificado              -----*/                    
/*----- Nombre           : SP_TWCLAS_CNTA_Q02                     -----*/                    
/*----- Script           : TWCLAS_CNTA                            -----*/                    
/*----- Desarrollado     : CEQN                                   -----*/                    
/*----- Fecha            : 10/06/2009                             -----*/                    
/*----- Base de Datos    : MS SQL Server                          -----*/                    
/*----- Version BD       : 8.0                                    -----*/                    
/*----- Invoca a SP      :                                        -----*/                    
/*---------------------------------------------------------------------*/                    
/*---------------------------------------------------------------------*/                    
/*----- Modificado 1 por :  PERCY GARCIA                                      -----*/                    
/*----- Fecha Modifica   :  15/08/2009                                      -----*/                    
/*----- Detalle Modifica :  CORRECCIONES DIVERSAS / OPTIMIZACIONES                                       -----*/                    
/*---------------------------------------------------------------------*/                    
/*----- Modificado 2 por :  Andres Soria                                    -----*/                             
/*----- Fecha Modifica   :  04/05/2010                                      -----*/                               
/*----- Detalle Modifica :  Se corrigio saldos para el mes 13               -----*/                            
    
/*---------------------------------------------------------------------*/   
/*---------------------------------------------------------------------*/                    
/*----- Drop Proc SP_TWCLAS_CNTA_Q02                              -----*/                    
/*----- Grant Execute On SP_TWCLAS_CNTA_Q02 To PUBLIC             -----*/                    
/*----- SP_TWCLAS_CNTA_Q02                                        -----*/                    
/*---------------------------------------------------------------------                    
 EXEC SP_TWCLAS_CNTA_Q02 '01', 1, '001', NULL, NULL, NULL, 2, 2, 'M', '', '01/01/2008', '31/12/2008', 2, ''               
  EXEC SP_TWCLAS_CNTA_Q02 '01', '001', 1,2009, 1, 1, 'M', 'REP',1,  NULL, NULL, 2, '', '1'               
---------------------------------------------------------------------*/                    
@ISCO_EMPR  TD_VC_002,                    
@ISCO_UNID_CNTB TD_VC_003,              
@INNU_CNTB_EMPR TD_IN_001_CONT,                
@INNU_ANNO  TD_IN_004_ANNO,                    
@INNU_MESE_0001 TD_IN_002_MESE,                    
@INNU_MESE_0002 TD_IN_002_MESE,                    
@ISTI_BUSQ  TD_VC_001,    -- M = Por Fechas J = Por Periodos              
@ISTI_PRES  TD_VC_003,    -- Tipo Presentacion REP / TAB                    
@INNV_PRES  TD_IN_001,    --Nivel Presentacion 1 / 1000 / 1000000                    
@ISFE_ASTO_INIC TD_VC_010,                
@ISFE_ASTO_FINA TD_VC_010,               
@INNU_DGTO      TD_IN_001,              
@ISCA_WHER  TD_VC_255,    -- Filtro              
@ISTI_ORDE  TD_VC_003   -- 1 = Ordenamiento por Fecha 2 = Ordenamiento por Operacion              
As                    
Declare                    
              
@CSCO_CNTA_EMPR  TD_VC_020,              
@VSNU_ANNO       TD_VC_004,                    
@VSNU_MESE_0001  TD_VC_002,                    
@VSNU_MESE_0002  TD_VC_002,                    
--@VSNU_DGTO       TD_VC_002,                    
@VSTI_BUSQ   TD_VC_255,              
   @VSTI_BUSQ_SALD   TD_VC_255,            
@VSFE_ASTO_INIC  TD_VC_010,              
@VSFE_ASTO_FINA  TD_VC_010,              
@VNNU_FETC_0001  TD_IN_001,              
@VNNU_CONT   TD_IN_001,                          
@VNNU_CONT_0001 TD_IN_001,              
@VNNU_CONT_0002 TD_IN_001,              
@VNNU_CONT_0003 TD_IN_001,              
@VSNU_CONT_0001 TD_VC_003,              
@VSCO_CLAS_0001 TD_VC_003,              
@VSCO_CLAS_CNTA TD_VC_003,              
@VSCO_CNTA_EMPR TD_VC_020,              
@VSCO_CNTA_0001 TD_VC_020,               
@VNNU_SEGU  TD_IN_001,              
@CSNU_ASTO  TD_VC_020,              
@CSCO_CLAS_CNTA TD_VC_003,              
@CSDE_CLAS_CNTA TD_VC_050,              
@CSNU_SECU_CLAS TD_VC_003,              
@CNNU_MESE  TD_IN_001,              
              
@VNCOLUMNA_1 TD_NU_016_004,               
@VNCOLUMNA_2 TD_NU_016_004,              
@VNCOLUMNA_3 TD_NU_016_004,               
@VNCOLUMNA_4 TD_NU_016_004,               
@VNCOLUMNA_5 TD_NU_016_004,               
@VNCOLUMNA_6 TD_NU_016_004,               
@VNCOLUMNA_7 TD_NU_016_004,               
@VNCOLUMNA_8 TD_NU_016_004,              
@VNCOLUMNA_9 TD_NU_016_004,               
@VNCOLUMNA_10 TD_NU_016_004,               
@VNCOLUMNA_11 TD_NU_016_004,               
@VNCOLUMNA_12 TD_NU_016_004,               
@VNCOLUMNA_13 TD_NU_016_004,               
@VNCOLUMNA_14 TD_NU_016_004,               
@VNCOLUMNA_15 TD_NU_016_004,               
@VNCOLUMNA_16 TD_NU_016_004,              
@VNCOLUMNA_17 TD_NU_016_004,               
@VNCOLUMNA_18 TD_NU_016_004,            
@CNNU_SECU_0001 Numeric(18)              
              
/*              
If Exists(Select * from dbo.sysobjects where id = object_id(N'[dbo.#TTCLAS_0001]') And objectproperty(id, N'IsTable') = 1 )              
 Drop Table #TTCLAS_0001              
*/              
/*              
Drop Table #TTCLAS_0001              
Drop Table #TTCLAS_0002              
Drop Table #TTCLAS_0003              
Drop Table #TTCLAS_0004              
Drop Table #TTCLAS_0005              
Drop Table #TTCLAS_0006              
Drop Table #TTCLAS_0007              
*/              
Create Table #TTCLAS_0003(              
 NU_ASTO Varchar(20) NULL,              
 FE_ASTO_CNTB DateTime NULL,              
 IM_MVTO_CNTB Numeric(16,4) NULL,              
 CO_CNTA_EMPR Varchar(20) NULL,              
 NV_CNTA_0001 Varchar(20) NULL,              
 CO_CLAS_CNTA Varchar(03) NULL,              
 NU_MESE Int ,              
NU_SECU Numeric(18) NULL ,            
CO_EMPR Varchar(2) NULL,             
NU_CNTB_EMPR Int NULL,             
CO_UNID_CNTB Varchar(3) NULL,            
SI_MVTO_CNTB  Varchar(3) NULL,            
NU_ANNO Int NULL,            
CO_OPRC_CNTB  Varchar(3) NULL,            
DE_GLOS Varchar(100) NULL,            
NU_ASTO_0001 Varchar(10) NULL,            
CO_CNTA_EM01  Varchar(20) NULL,          
)              
              
Create Table #TTCLAS_0001(              
 NU_ASTO Varchar(20) NULL,              
 IM_MVTO_CNTB Numeric(16,4) NULL,              
 CO_CNTA_EMPR Varchar(20) NULL,              
 CO_CLAS_CNTA Varchar(03) NULL,              
 NU_MESE Int,             
NU_SECU Numeric(18) NULL              
)              
              
Create Table #TTCLAS_0002(              
 NU_ASTO Varchar(20) NULL,              
 FE_ASTO_CNTB Varchar(10) NULL,              
 DE_GLOS Varchar(100) NULL,              
 CO_CLAS_CNTA Varchar(03) NULL, --SE VA EN UPDATE PRIMER CURSOR              
 NU_CONT Varchar(03) NULL,  --SE VA EN UPDATE PRIMER CURSOR              
 NU_FILA Int IDENTITY(1,1) NOT NULL,              
 NU_MESE Int              
)              
              
Create Table #TTCLAS_0006(              
 NU_ASTO Varchar(20) NULL,              
 FE_ASTO_CNTB Varchar(10) NULL,              
 DE_GLOS Varchar(100) NULL,              
 --CO_CLAS_CNTA Varchar(03) NULL,              
 NU_CONT Int NULL,              
 NU_SEGU Int identity(1,1),              
 COLUMNA_1 Numeric(16,4) NULL,              
 COLUMNA_2 Numeric(16,4) NULL,              
 COLUMNA_3 Numeric(16,4) NULL,              
 COLUMNA_4 Numeric(16,4) NULL,              
 COLUMNA_5 Numeric(16,4) NULL,              
 COLUMNA_6 Numeric(16,4) NULL,              
 COLUMNA_7 Numeric(16,4) NULL,              
 COLUMNA_8 Numeric(16,4) NULL,    
 COLUMNA_9 Numeric(16,4) NULL,              
 COLUMNA_10 Numeric(16,4) NULL,              
 COLUMNA_11 Numeric(16,4) NULL,              
 COLUMNA_12 Numeric(16,4) NULL,              
 COLUMNA_13 Numeric(16,4) NULL,              
 COLUMNA_14 Numeric(16,4) NULL,              
 COLUMNA_15 Numeric(16,4) NULL,              
 COLUMNA_16 Numeric(16,4) NULL,              
 COLUMNA_17 Numeric(16,4) NULL,              
 COLUMNA_18 Numeric(16,4) NULL              
)              
              
Create Table #TTCLAS_0005(              
 NU_CONT Int NULL,              
 NU_COLU Numeric(16,4) NULL ,            
 CO_CNTA_EMPR Varchar(20) NULL               
)              
              
Create Table #TTCLAS_0004(              
 NU_ASTO Varchar(20) NULL,              
NU_SECU Numeric(18) NULL,            
 FE_ASTO_CNTB Varchar(10) NULL,              
 DE_GLOS Varchar(100) NULL,              
 --CO_CLAS_CNTA Varchar(03) NULL,              
 NU_CONT Int NULL,              
 NU_SEGU Int identity(1,1),               
 DE_CLAS_CNTA Varchar(50) NULL,              
 CO_CNTA_EMPR Varchar(20) NULL,              
 NU_SECU_CLAS Varchar(03) NULL,              
 NU_SECU_QUIE Int NULL,              
 NU_MESE Int NULL,              
 COLUMNA_1 Numeric(16,4) NULL,              
 COLUMNA_2 Numeric(16,4) NULL,              
 COLUMNA_3 Numeric(16,4) NULL,              
 COLUMNA_4 Numeric(16,4) NULL,              
 COLUMNA_5 Numeric(16,4) NULL,              
 COLUMNA_6 Numeric(16,4) NULL,              
 COLUMNA_7 Numeric(16,4) NULL,              
 COLUMNA_8 Numeric(16,4) NULL,              
 COLUMNA_9 Numeric(16,4) NULL,              
 COLUMNA_10 Numeric(16,4) NULL,              
 COLUMNA_11 Numeric(16,4) NULL,              
 COLUMNA_12 Numeric(16,4) NULL,              
 COLUMNA_13 Numeric(16,4) NULL,              
 COLUMNA_14 Numeric(16,4) NULL,              
 COLUMNA_15 Numeric(16,4) NULL,              
 COLUMNA_16 Numeric(16,4) NULL,              
 COLUMNA_17 Numeric(16,4) NULL,              
 COLUMNA_18 Numeric(16,4) NULL,                         
 DE_VIEN   Varchar(20) NULL              
)              
  /*            
Create Table #TTCLAS_0007(              
 COLUMNA_1 Numeric(16,4) NULL,              
 COLUMNA_2 Numeric(16,4) NULL,              
 COLUMNA_3 Numeric(16,4) NULL,              
 COLUMNA_4 Numeric(16,4) NULL,              
 COLUMNA_5 Numeric(16,4) NULL,              
 COLUMNA_6 Numeric(16,4) NULL,              
 COLUMNA_7 Numeric(16,4) NULL,              
 COLUMNA_8 Numeric(16,4) NULL,              
 COLUMNA_9 Numeric(16,4) NULL,              
 COLUMNA_10 Numeric(16,4) NULL,              
 COLUMNA_11 Numeric(16,4) NULL,              
 COLUMNA_12 Numeric(16,4) NULL,              
 COLUMNA_13 Numeric(16,4) NULL,              
 COLUMNA_14 Numeric(16,4) NULL,              
 COLUMNA_15 Numeric(16,4) NULL,              
 COLUMNA_16 Numeric(16,4) NULL,              
 COLUMNA_17 Numeric(16,4) NULL,              
 COLUMNA_18 Numeric(16,4) NULL              
)             
*/             
              
CREATE TABLE #TTCLAS_8888(                
 [MONTO]   Numeric(16,4) NULL,                
 [NU_ASTO] VARCHAR(30) NULL,                
 [FECHA]   varchar(50) NULL,                
 [DESCRIPCION] VARCHAR(200),                
 [DE_GLOS] VARCHAR(200),                
 [NU_CONT] INT NULL,                
 [NU_SEGU] INT NULL,                
 [NU_ANNO] INT NULL,                
 [NU_MESE] INT NULL,                
 [NU_SECU_QUIE] INT NULL                
)             
             
Select @VSNU_ANNO = Convert(Varchar(4),@INNU_ANNO )                    
Select @VSNU_MESE_0001 = Convert(Varchar(2),@INNU_MESE_0001 )                    
Select @VSNU_MESE_0002 = Convert(Varchar(2),@INNU_MESE_0002 )                    
Select @VSTI_BUSQ = ''                  
              
If @ISTI_BUSQ = 'M'              
BEGIN            
 Select @VSTI_BUSQ = ' And Convert(DateTime, Convert(Varchar,TXMVTO_CNTB.FE_ASTO_CNTB, 110), 110) between ' +               
      ' Convert(DateTime, Convert(Varchar,"' + @ISFE_ASTO_INIC + '", 110), 110) And Convert(DateTime, Convert(Varchar,"' + @ISFE_ASTO_FINA + '", 110), 110)'                
Select @VSTI_BUSQ_SALD = ''               
END            
ELSE              
BEGIN            
 Select @VSTI_BUSQ = ' And TXMVTO_CNTB.NU_ANNO = "' + @VSNU_ANNO + '" and TXMVTO_CNTB.NU_MESE between "' + @VSNU_MESE_0001 + '" and "' + @VSNU_MESE_0002 + '"'               
  END            
              
/**********/               
               
Exec( ' Insert Into #TTCLAS_0003              
Select  TXMVTO_CNTB.CO_UNID_CNTB +  TXMVTO_CNTB.CO_OPRC_CNTB + TXMVTO_CNTB.NU_ASTO,               
  TXMVTO_CNTB.FE_ASTO_CNTB,              
  (Case When TI_OPER = "CAR" Then IM_MVTO_CNTB Else (-1)*IM_MVTO_CNTB End),              
  Substring(TXMVTO_CNTB.CO_CNTA_EMPR,1, ' + @INNU_DGTO + '),              
  Substring(TMPLAN_EMPR.CO_CNTA_EMPR,1, ' + @INNU_DGTO + '),              
  TMPLAN_EMPR.CO_CLAS_CNTA, TXMVTO_CNTB.NU_MESE , TXMVTO_CNTB.NU_SECU,             
  TXMVTO_CNTB.CO_EMPR,TXMVTO_CNTB.NU_CNTB_EMPR,  TXMVTO_CNTB.CO_UNID_CNTB,              
  TXMVTO_CNTB.SI_MVTO_CNTB,TXMVTO_CNTB.NU_ANNO,TXMVTO_CNTB.CO_OPRC_CNTB,TXMVTO_CNTB.DE_GLOS,          
  TXMVTO_CNTB.NU_ASTO,            
  TXMVTO_CNTB.CO_CNTA_EMPR            
From TMPLAN_EMPR              
Left Outer Join TXMVTO_CNTB               
 On TMPLAN_EMPR.CO_EMPR = TXMVTO_CNTB.CO_EMPR              
 And  TMPLAN_EMPR.CO_CNTA_EMPR = TXMVTO_CNTB.CO_CNTA_EMPR              
Where Substring(TMPLAN_EMPR.CO_CNTA_EMPR, 1, ' + @INNU_DGTO + ') Like Substring(TXMVTO_CNTB.CO_CNTA_EMPR, 1, ' + @INNU_DGTO + ') + "%"               
AND TXMVTO_CNTB.CO_EMPR =  "' + @ISCO_EMPR + '"                
AND TXMVTO_CNTB.NU_CNTB_EMPR = ' + @INNU_CNTB_EMPR + '  ' + @VSTI_BUSQ + '  '               
+ @ISCA_WHER + '             
And Substring(TXMVTO_CNTB.CO_CNTA_EMPR,1, ' + @INNU_DGTO + ') In(Select Substring(t1.CO_CNTA_EMPR,1, ' + @INNU_DGTO + ') From TMPLAN_EMPR t1, TTCLAS_CNTA t2              
             Where t1.CO_EMPR = t2.CO_EMPR              
             And t1.CO_CLAS_CNTA = t2.CO_CLAS_CNTA              
            )              
And TXMVTO_CNTB.SI_MVTO_CNTB In ( "APR")              
')              
      
      
           
Insert Into #TTCLAS_0001              
Select  #TTCLAS_0003.NU_ASTO,               
  Sum(#TTCLAS_0003.IM_MVTO_CNTB),               
  #TTCLAS_0003.CO_CNTA_EMPR,              
  Max(#TTCLAS_0003.CO_CLAS_CNTA),              
  #TTCLAS_0003.NU_MESE,  #TTCLAS_0003.NU_SECU              
From #TTCLAS_0003              
Group By #TTCLAS_0003.CO_CNTA_EMPR, #TTCLAS_0003.NU_ASTO,#TTCLAS_0003.NU_SECU, #TTCLAS_0003.NU_MESE              
          
              
        
              
/********************************************************************/              
              
Select @VNNU_CONT = 0              
Select @VNNU_CONT_0001 = 0              
Select @VNNU_CONT_0002 = 0              
Select @VSCO_CLAS_CNTA = ''              
Select @VSCO_CLAS_0001 = ''              
       
/********************************************************************/              
              
Select @VNNU_CONT = 0              
Select @VNNU_CONT_0001 = 0              
Select @VNNU_CONT_0002 = 0              
Select @VNNU_CONT_0003 = 0              
Select @VSCO_CLAS_CNTA = ''              
Select @VSCO_CLAS_0001 = ''              
Select @VSCO_CNTA_EMPR = ''              
Select @VSCO_CNTA_0001 = ''              
Select @VSCO_CNTA_0001 = ''              
SELECT @VNNU_SEGU = 0              
              
Declare CU_CLAS_CNTA_Q01_2 Cursor For                
 SELECT  #TTCLAS_0001.CO_CNTA_EMPR, #TTCLAS_0001.NU_ASTO, #TTCLAS_0001.CO_CLAS_CNTA              
 FROM #TTCLAS_0001               
 Order By 1, 2               
              
Open CU_CLAS_CNTA_Q01_2             
Fetch CU_CLAS_CNTA_Q01_2 Into @CSCO_CNTA_EMPR, @CSNU_ASTO, @CSCO_CLAS_CNTA              
Select @VNNU_FETC_0001  = @@FETCH_STATUS                
While (@VNNU_FETC_0001  = 0)                
  BEGIN              
      IF (@VNNU_FETC_0001   < 0)                
  BEGIN                
   --RAISERROR 20001 "FIN DE ARCHIVO"                
   CLOSE CU_CLAS_CNTA_Q01_2                
   DEALLOCATE CU_CLAS_CNTA_Q01_2                
   RETURN                
        END                
              
  /**********************************/              
              
  SELECT @VSCO_CNTA_0001 = @CSCO_CNTA_EMPR              
              
  SELECT @VNNU_CONT_0002 = @VNNU_CONT_0002 + 1              
       
  If (@VSCO_CLAS_CNTA != @CSCO_CLAS_CNTA Or @VNNU_CONT_0003 >= 10)--19)               
   Begin              
    Select @VNNU_CONT_0003 = @VNNU_CONT_0003 + 1              
    Select @VNNU_CONT_0001 = @VNNU_CONT_0001 + 1              
              
    If @VNNU_CONT_0003 = 10---19               
     Begin              
      Select @VNNU_CONT_0003 = 0              
End              
                  
    INSERT INTO #TTCLAS_0006(NU_ASTO,FE_ASTO_CNTB,DE_GLOS,NU_CONT)              
    Values(NULL, NULL, NULL, @VNNU_CONT_0001)               
              
    SELECT  @VNNU_SEGU = @VNNU_SEGU + 1              
    SELECT @VNNU_CONT = 0              
              
    SELECT @VSCO_CLAS_CNTA = @CSCO_CLAS_CNTA              
    SELECT @VSCO_CNTA_0001 = @CSCO_CNTA_EMPR              
              
   End              
              
  If @VSCO_CNTA_EMPR != @CSCO_CNTA_EMPR               
   Begin              
    SELECT @VNNU_CONT = @VNNU_CONT + 1              
    SELECT @VSCO_CNTA_EMPR = @CSCO_CNTA_EMPR       
    -- actualizar la columna nueva con CUENTA              
    If @VNNU_CONT > 9--18              
     Begin              
      Select @VNNU_CONT = 1        
      Select @VNNU_CONT_0001 = @VNNU_CONT_0001 + 1              
              
      INSERT INTO #TTCLAS_0006(NU_ASTO,FE_ASTO_CNTB,DE_GLOS,NU_CONT)              
      Values(NULL, NULL, NULL, @VNNU_CONT_0001)               
      SELECT  @VNNU_SEGU = @VNNU_SEGU + 1         
     End              
              
    If @VNNU_CONT > 0 AND @VNNU_CONT <= 9--18              
     Begin              
      Exec( 'UPDATE #TTCLAS_0006 SET [COLUMNA_' + @VNNU_CONT + '] = Convert(Int, ' + @VSCO_CNTA_EMPR + ') ' +               
      ' WHERE NU_CONT = ' + @VNNU_SEGU)              
     End              
   End               
              
   IF (@@ERROR != 0)                
   Begin                
    Close CU_CLAS_CNTA_Q01_2                
    Deallocate CU_CLAS_CNTA_Q01_2                
    Return                
   End               
              
  /**********************************/              
              
    Fetch CU_CLAS_CNTA_Q01_2 Into @CSCO_CNTA_EMPR, @CSNU_ASTO, @CSCO_CLAS_CNTA              
              
 Select @VNNU_FETC_0001 = @@FETCH_STATUS              
              
  END                
                
Close CU_CLAS_CNTA_Q01_2                
Deallocate CU_CLAS_CNTA_Q01_2                
              
/********************************************************************/              
              
              
              
Declare CU_CLAS_CNTA_Q01_3 Cursor For                
 Select NU_CONT, COLUMNA_1, COLUMNA_2, COLUMNA_3, COLUMNA_4, COLUMNA_5, COLUMNA_6, COLUMNA_7, COLUMNA_8,              
 COLUMNA_9, COLUMNA_10, COLUMNA_11, COLUMNA_12, COLUMNA_13, COLUMNA_14, COLUMNA_15, COLUMNA_16, COLUMNA_17, COLUMNA_18              
 From #TTCLAS_0006 where nu_asto is null              
              
Open CU_CLAS_CNTA_Q01_3                
              
Fetch CU_CLAS_CNTA_Q01_3 Into @VNNU_CONT, @VNCOLUMNA_1, @VNCOLUMNA_2, @VNCOLUMNA_3, @VNCOLUMNA_4, @VNCOLUMNA_5, @VNCOLUMNA_6, @VNCOLUMNA_7, @VNCOLUMNA_8,              
 @VNCOLUMNA_9, @VNCOLUMNA_10, @VNCOLUMNA_11, @VNCOLUMNA_12, @VNCOLUMNA_13, @VNCOLUMNA_14, @VNCOLUMNA_15, @VNCOLUMNA_16, @VNCOLUMNA_17, @VNCOLUMNA_18              
              
Select @VNNU_FETC_0001  = @@FETCH_STATUS                
                
While (@VNNU_FETC_0001  = 0)                
  BEGIN              
      IF (@VNNU_FETC_0001   < 0)                
  BEGIN                
   --RAISERROR 20001 "FIN DE ARCHIVO"                
   CLOSE CU_CLAS_CNTA_Q01_3                
   DEALLOCATE CU_CLAS_CNTA_Q01_3                
   RETURN                
        END                
              
  /**********************************/              
              
  INSERT INTO #TTCLAS_0005 (NU_CONT, NU_COLU)              
  VALUES ( @VNNU_CONT, @VNCOLUMNA_1)              
                
  INSERT INTO #TTCLAS_0005 (NU_CONT, NU_COLU)              
  VALUES ( @VNNU_CONT, @VNCOLUMNA_2)              
              
  INSERT INTO #TTCLAS_0005 (NU_CONT, NU_COLU)              
  VALUES ( @VNNU_CONT, @VNCOLUMNA_3)              
              
  INSERT INTO #TTCLAS_0005 (NU_CONT, NU_COLU)              
  VALUES ( @VNNU_CONT, @VNCOLUMNA_4)              
              
  INSERT INTO #TTCLAS_0005 (NU_CONT, NU_COLU)              
  VALUES ( @VNNU_CONT, @VNCOLUMNA_5)              
              
  INSERT INTO #TTCLAS_0005 (NU_CONT, NU_COLU)              
  VALUES ( @VNNU_CONT, @VNCOLUMNA_6)              
              
  INSERT INTO #TTCLAS_0005 (NU_CONT, NU_COLU)              
  VALUES ( @VNNU_CONT, @VNCOLUMNA_7)              
              
  INSERT INTO #TTCLAS_0005 (NU_CONT, NU_COLU)              
  VALUES ( @VNNU_CONT, @VNCOLUMNA_8)              
              
  INSERT INTO #TTCLAS_0005 (NU_CONT, NU_COLU)              
  VALUES ( @VNNU_CONT, @VNCOLUMNA_9)              
              
  INSERT INTO #TTCLAS_0005 (NU_CONT, NU_COLU)              
  VALUES ( @VNNU_CONT, @VNCOLUMNA_10)              
              
  INSERT INTO #TTCLAS_0005 (NU_CONT, NU_COLU)              
  VALUES ( @VNNU_CONT, @VNCOLUMNA_11)              
              
  INSERT INTO #TTCLAS_0005 (NU_CONT, NU_COLU)              
  VALUES ( @VNNU_CONT, @VNCOLUMNA_12)              
              
  INSERT INTO #TTCLAS_0005 (NU_CONT, NU_COLU)              
  VALUES ( @VNNU_CONT, @VNCOLUMNA_13)              
              
  INSERT INTO #TTCLAS_0005 (NU_CONT, NU_COLU)              
  VALUES ( @VNNU_CONT, @VNCOLUMNA_14)              
              
  INSERT INTO #TTCLAS_0005 (NU_CONT, NU_COLU)              
  VALUES ( @VNNU_CONT, @VNCOLUMNA_15)              
              
  INSERT INTO #TTCLAS_0005 (NU_CONT, NU_COLU)              
  VALUES ( @VNNU_CONT, @VNCOLUMNA_16)              
              
  INSERT INTO #TTCLAS_0005 (NU_CONT, NU_COLU)              
  VALUES ( @VNNU_CONT, @VNCOLUMNA_17)              
              
  INSERT INTO #TTCLAS_0005 (NU_CONT, NU_COLU)              
  VALUES ( @VNNU_CONT, @VNCOLUMNA_18)              
              
IF (@@ERROR != 0)                
  Begin                
   Close CU_CLAS_CNTA_Q01_3                
   Deallocate CU_CLAS_CNTA_Q01_3                
   Return                
  End               
              
  /**********************************/              
              
    Fetch CU_CLAS_CNTA_Q01_3 Into @VNNU_CONT, @VNCOLUMNA_1, @VNCOLUMNA_2, @VNCOLUMNA_3, @VNCOLUMNA_4, @VNCOLUMNA_5, @VNCOLUMNA_6, @VNCOLUMNA_7, @VNCOLUMNA_8,              
  @VNCOLUMNA_9, @VNCOLUMNA_10, @VNCOLUMNA_11, @VNCOLUMNA_12, @VNCOLUMNA_13, @VNCOLUMNA_14, @VNCOLUMNA_15, @VNCOLUMNA_16, @VNCOLUMNA_17, @VNCOLUMNA_18              
              
 Select @VNNU_FETC_0001 = @@FETCH_STATUS              
              
  END                
                
Close CU_CLAS_CNTA_Q01_3                
Deallocate CU_CLAS_CNTA_Q01_3                
            
Update #TTCLAS_0005            
Set CO_CNTA_EMPR = Case When Len(Convert(Varchar, Convert(NUMERIC(16,0), NU_COLU))) != @INNU_DGTO Then Convert(Varchar,'0'+Convert(Varchar,Convert(NUMERIC(16,0), NU_COLU)))            
Else Convert(Varchar, Convert(NUMERIC(16,0), NU_COLU)) End             
Where NU_COLU IS NOT NULL            
              
-----Se Agrego      
DELETE FROM #TTCLAS_0005 WHERE NU_COLU IS NULL AND CO_CNTA_EMPR IS NULL                 
      
/********************************************************************/              
              
Select @VNNU_CONT = 0              
Select @VNNU_CONT_0001 = 0              
Select @VNNU_CONT_0002 = 0              
Select @VNNU_CONT_0003 = 0              
Select @VSCO_CLAS_CNTA = ''              
Select @VSCO_CLAS_0001 = ''              
Select @VSCO_CNTA_EMPR = ''              
Select @VSCO_CNTA_0001 = ''              
Select @CNNU_MESE = ''              
SELECT @VNNU_SEGU = 0              
            
Create Table #TWMESE_FCOERDG(NU_MESE Int NULL)            
            
Insert Into #TWMESE_FCOERDG(NU_MESE)            
 SELECT  #TTCLAS_0001.NU_MESE             
 FROM #TTCLAS_0001            
 Group By NU_MESE            
            
Declare CU_CLAS_CNTA_Q01_4 Cursor For                
 SELECT  #TTCLAS_0001.CO_CNTA_EMPR, #TTCLAS_0001.NU_ASTO, #TTCLAS_0001.CO_CLAS_CNTA, TTCLAS_CNTA.DE_CLAS_CNTA,              
    TTCLAS_CNTA.NU_SECU_CLAS,  #TTCLAS_0001.NU_MESE , #TTCLAS_0001.NU_SECU             
 FROM #TTCLAS_0001, TTCLAS_CNTA              
 Where #TTCLAS_0001.CO_CLAS_CNTA = TTCLAS_CNTA.CO_CLAS_CNTA
 And   TTCLAS_CNTA.CO_EMPR = @ISCO_EMPR               
 -- Se Agrego  Order By 1, 2              
   Order By TTCLAS_CNTA.NU_SECU_CLAS, 1, 2             
             
Open CU_CLAS_CNTA_Q01_4              
              
Fetch CU_CLAS_CNTA_Q01_4 Into @CSCO_CNTA_EMPR, @CSNU_ASTO, @CSCO_CLAS_CNTA, @CSDE_CLAS_CNTA, @CSNU_SECU_CLAS, @CNNU_MESE,            
@CNNU_SECU_0001                
              
Select @VNNU_FETC_0001  = @@FETCH_STATUS     
                
While (@VNNU_FETC_0001  = 0)                
  BEGIN              
      IF (@VNNU_FETC_0001   < 0)                
  BEGIN                
   --RAISERROR 20001 "FIN DE ARCHIVO"                
   CLOSE CU_CLAS_CNTA_Q01_4                
   DEALLOCATE CU_CLAS_CNTA_Q01_4                
   RETURN                
        END                
              
  /**********************************/              
              
  SELECT @VSCO_CNTA_0001 = @CSCO_CNTA_EMPR               
              
  Select @VNNU_CONT_0002 = @VNNU_CONT_0002 + 1              
      
  If (@VSCO_CLAS_CNTA != @CSCO_CLAS_CNTA Or @VNNU_CONT_0003 >= 10)--19)               
   Begin              
    Select @VNNU_CONT_0003 = @VNNU_CONT_0003 + 1              
    Select @VNNU_CONT_0001 = @VNNU_CONT_0001 + 1              
              
    If @VNNU_CONT_0003 = 10--19               
     Begin              
      Select @VNNU_CONT_0003 = 0              
     End              
                  
    INSERT INTO #TTCLAS_0004(NU_ASTO,FE_ASTO_CNTB,DE_GLOS,NU_CONT, DE_CLAS_CNTA, CO_CNTA_EMPR, NU_SECU_CLAS, NU_SECU_QUIE, NU_MESE)              
--    Values(NULL, NULL, NULL, @VNNU_CONT_0001, @CSDE_CLAS_CNTA, NULL,@CSNU_SECU_CLAS, @VNNU_CONT_0001, NULL )               
   Select NULL, NULL, NULL, @VNNU_CONT_0001, @CSDE_CLAS_CNTA, NULL,@CSNU_SECU_CLAS, @VNNU_CONT_0001, NU_MESE            
   From #TWMESE_FCOERDG            
              
    Exec( ' INSERT INTO #TTCLAS_0004 (NU_ASTO, FE_ASTO_CNTB, DE_GLOS, DE_CLAS_CNTA, CO_CNTA_EMPR, NU_SECU_CLAS, NU_SECU_QUIE, NU_MESE,NU_SECU)              
    Select  TXMVTO_CNTB.CO_UNID_CNTB +  TXMVTO_CNTB.CO_OPRC_CNTB + TXMVTO_CNTB.NU_ASTO_0001,              
       Convert(Varchar(10),Max(TXMVTO_CNTB.FE_ASTO_CNTB),103),              
       Max(TXMVTO_CNTB.DE_GLOS), Max(DE_CLAS_CNTA), Substring(TXMVTO_CNTB.CO_CNTA_EM01,1, ' + @INNU_DGTO + '), MAX(NU_SECU_CLAS), ' + @VNNU_CONT_0001 + ',               
       TXMVTO_CNTB.NU_MESE ,TXMVTO_CNTB.NU_SECU              
       From  #TTCLAS_0003 AS TXMVTO_CNTB              
           Left Outer Join TMPLAN_EMPR              
           On TMPLAN_EMPR.CO_EMPR = TXMVTO_CNTB.CO_EMPR              
           And  TMPLAN_EMPR.CO_CNTA_EMPR = TXMVTO_CNTB.CO_CNTA_EM01              
           Left Outer Join TTCLAS_CNTA              
           On TTCLAS_CNTA.CO_EMPR = TMPLAN_EMPR.CO_EMPR              
     And  TTCLAS_CNTA.CO_CLAS_CNTA = TMPLAN_EMPR.CO_CLAS_CNTA              
           Left Outer Join TMUNID_CNTB              
           On TMUNID_CNTB.CO_EMPR = TXMVTO_CNTB.CO_EMPR          
           And  TMUNID_CNTB.CO_UNID_CNTB = TXMVTO_CNTB.CO_UNID_CNTB              
       Where TXMVTO_CNTB.CO_EMPR = "' + @ISCO_EMPR + '"                
       AND   TXMVTO_CNTB.NU_CNTB_EMPR = ' + @INNU_CNTB_EMPR + '              
       And TXMVTO_CNTB.SI_MVTO_CNTB In ( "APR")     '              
        + ' ' + @VSTI_BUSQ + '                
       And Substring(TXMVTO_CNTB.CO_CNTA_EM01,1, ' + @INNU_DGTO + ') In(Select Substring(t1.CO_CNTA_EMPR,1, ' + @INNU_DGTO + ') From TMPLAN_EMPR t1, TTCLAS_CNTA t2              
                    Where t1.CO_EMPR = t2.CO_EMPR              
                    And t1.CO_CLAS_CNTA = t2.CO_CLAS_CNTA              
                   )              
       And Substring(TXMVTO_CNTB.CO_CNTA_EM01,1, ' + @INNU_DGTO + ') In(SELECT SUBSTRING(Convert(Varchar(20), #TTCLAS_0005.CO_CNTA_EMPR), 1, ' + @INNU_DGTO + ')              
               FROM #TTCLAS_0005                
               Where #TTCLAS_0005.NU_COLU IS NOT NULL AND #TTCLAS_0005.NU_CONT = ' + @VNNU_CONT_0001 + '              
               )              
       And TMPLAN_EMPR.CO_CLAS_CNTA = "' + @CSCO_CLAS_CNTA + '"               
      Group By TXMVTO_CNTB.CO_UNID_CNTB, TXMVTO_CNTB.CO_OPRC_CNTB, TXMVTO_CNTB.NU_ASTO_0001, TXMVTO_CNTB.NU_SECU,Substring(TXMVTO_CNTB.CO_CNTA_EM01,1, ' + @INNU_DGTO + '), TXMVTO_CNTB.NU_MESE')         
             
  IF (@@ERROR != 0)                
    Begin                
     Close CU_CLAS_CNTA_Q01_4                
     Deallocate CU_CLAS_CNTA_Q01_4                
     Return                
    End               
-- Inserta saldos de cada cuenta por mes                     
------------------------------------------------------------------------------------------------                
                
     Exec( 'INSERT Into #TTCLAS_8888 (MONTO, NU_ASTO, NU_SEGU, NU_ANNO, NU_MESE, NU_SECU_QUIE)                         
      Select (SUM(TASALD_EMPR.IM_CARG_CNTB) - SUM(TASALD_EMPR.IM_ABON_CNTB)), TASALD_EMPR.CO_CNTA_EMPR,                          
       MAX(TTCLAS_CNTA.NU_SECU_CLAS),TASALD_EMPR.NU_ANNO, TASALD_EMPR.NU_MESE, ' + @VNNU_CONT_0001 + '                        
      From  TASALD_EMPR                         
        Left Outer Join TMPLAN_EMPR                          
        On TMPLAN_EMPR.CO_EMPR = TASALD_EMPR.CO_EMPR                          
        And  TMPLAN_EMPR.CO_CNTA_EMPR = TASALD_EMPR.CO_CNTA_EMPR                          
        Left Outer Join TTCLAS_CNTA                          
        On TTCLAS_CNTA.CO_EMPR = TMPLAN_EMPR.CO_EMPR                       
        And  TTCLAS_CNTA.CO_CLAS_CNTA = TMPLAN_EMPR.CO_CLAS_CNTA                          
        Left Outer Join TMUNID_CNTB                          
        On TMUNID_CNTB.CO_EMPR = TASALD_EMPR.CO_EMPR                          
        And  TMUNID_CNTB.CO_UNID_CNTB = TASALD_EMPR.CO_UNID_CNTB                          
     Where TASALD_EMPR.CO_EMPR = "' + @ISCO_EMPR + '"                    
       AND TASALD_EMPR.NU_ANNO= ' + @INNU_ANNO + '                    
       AND TASALD_EMPR.NU_MESE <= ' + @INNU_MESE_0002 + '                    
       AND   TASALD_EMPR.NU_CNTB_EMPR = ' + @INNU_CNTB_EMPR + ' '+ @ISCA_WHER + '                              
       And Len(TASALD_EMPR.CO_CNTA_EMPR) = ' + @INNU_DGTO + '                     
       and TASALD_EMPR.TI_AUXI_EMPR IS NULL                
       AND TASALD_EMPR.CO_AUXI_EMPR IS NULL                
          AND TASALD_EMPR.CO_ORDE_SERV IS NULL                  
       And TASALD_EMPR.CO_CNTA_EMPR In(Select CO_CNTA_EMPR From TMPLAN_EMPR t1, TTCLAS_CNTA t2                            
           Where t1.CO_EMPR = t2.CO_EMPR                            
           And t1.CO_CLAS_CNTA = t2.CO_CLAS_CNTA )                            
--       And TASALD_EMPR.CO_CNTA_EMPR In(SELECT #TTCLAS_0005.CO_CNTA_EMPR                          
--      FROM #TTCLAS_0005                              
--        Where #TTCLAS_0005.NU_COLU IS NOT NULL AND #TTCLAS_0005.NU_CONT = ' + @VNNU_CONT_0001 + ' )                    
    And TMPLAN_EMPR.CO_CLAS_CNTA = "' + @CSCO_CLAS_CNTA + '"                      
      Group By TASALD_EMPR.CO_UNID_CNTB, TASALD_EMPR.CO_CNTA_EMPR , TASALD_EMPR.NU_MESE, TASALD_EMPR.NU_ANNO ')                         
-----------------------------------------------------------------------------------------------------------------              
 -- NO TENIA MES DESDE        
      
    SELECT  @VNNU_SEGU = @VNNU_SEGU + 1              
    Select @VNNU_CONT = 0              
              
    Select @VSCO_CLAS_CNTA = @CSCO_CLAS_CNTA              
    Select @VSCO_CNTA_0001 = @CSCO_CNTA_EMPR              
              
   End              
              
   If @VSCO_CNTA_EMPR != @CSCO_CNTA_EMPR               
    Begin              
     Select @VNNU_CONT = @VNNU_CONT + 1              
     Select @VSCO_CNTA_EMPR = @CSCO_CNTA_EMPR              
              
     -- actualizar la columna nueva con CUENTA              
     If @VNNU_CONT > 9--18              
      Begin              
       Select @VNNU_CONT = 1              
       Select @VNNU_CONT_0001 = @VNNU_CONT_0001 + 1              
              
       INSERT INTO #TTCLAS_0004(NU_ASTO,FE_ASTO_CNTB,DE_GLOS,NU_CONT, DE_CLAS_CNTA, CO_CNTA_EMPR,NU_SECU_CLAS, NU_SECU_QUIE, NU_MESE)              
  --   Values(NULL, NULL, NULL, @VNNU_CONT_0001, @CSDE_CLAS_CNTA, NULL, @CSNU_SECU_CLAS, @VNNU_CONT_0001, NULL)               
 Select NULL, NULL, NULL, @VNNU_CONT_0001, @CSDE_CLAS_CNTA, NULL,@CSNU_SECU_CLAS, @VNNU_CONT_0001, NU_MESE            
   From #TWMESE_FCOERDG            
              
       SELECT  @VNNU_SEGU = @VNNU_SEGU + 1              
              
       Exec( ' INSERT INTO #TTCLAS_0004 (NU_ASTO, FE_ASTO_CNTB, DE_GLOS, DE_CLAS_CNTA, CO_CNTA_EMPR, NU_SECU_CLAS, NU_SECU_QUIE, NU_MESE,NU_SECU)              
       Select  TXMVTO_CNTB.CO_UNID_CNTB +  TXMVTO_CNTB.CO_OPRC_CNTB + TXMVTO_CNTB.NU_ASTO_0001,              
          Convert(Varchar(10),Max(TXMVTO_CNTB.FE_ASTO_CNTB),103),              
          Max(TXMVTO_CNTB.DE_GLOS), Max(DE_CLAS_CNTA), Substring(TXMVTO_CNTB.CO_CNTA_EM01,1, ' + @INNU_DGTO + '), MAX(NU_SECU_CLAS), ' + @VNNU_CONT_0001 + ',               
          TXMVTO_CNTB.NU_MESE,TXMVTO_CNTB.NU_SECU              
          From  #TTCLAS_0003 AS TXMVTO_CNTB              
              Left Outer Join TMPLAN_EMPR              
              On TMPLAN_EMPR.CO_EMPR = TXMVTO_CNTB.CO_EMPR              
              And  TMPLAN_EMPR.CO_CNTA_EMPR = TXMVTO_CNTB.CO_CNTA_EM01              
              Left Outer Join TTCLAS_CNTA              
              On TTCLAS_CNTA.CO_EMPR = TMPLAN_EMPR.CO_EMPR              
              And  TTCLAS_CNTA.CO_CLAS_CNTA = TMPLAN_EMPR.CO_CLAS_CNTA              
              Left Outer Join TMUNID_CNTB              
              On TMUNID_CNTB.CO_EMPR = TXMVTO_CNTB.CO_EMPR              
              And  TMUNID_CNTB.CO_UNID_CNTB = TXMVTO_CNTB.CO_UNID_CNTB              
          Where TXMVTO_CNTB.CO_EMPR = "' + @ISCO_EMPR + '"                
          AND   TXMVTO_CNTB.NU_CNTB_EMPR = ' + @INNU_CNTB_EMPR + '              
          And TXMVTO_CNTB.CO_UNID_CNTB = "' + @ISCO_UNID_CNTB + '"              
          And TXMVTO_CNTB.SI_MVTO_CNTB In ( "APR")     '              
         + ' ' + @VSTI_BUSQ + '                
         And Substring(TXMVTO_CNTB.CO_CNTA_EM01,1, ' + @INNU_DGTO + ') In(Select Substring(t1.CO_CNTA_EMPR,1, ' + @INNU_DGTO + ') From TMPLAN_EMPR t1, TTCLAS_CNTA t2              
                       Where t1.CO_EMPR = t2.CO_EMPR              
                       And t1.CO_CLAS_CNTA = t2.CO_CLAS_CNTA              
                      )              
          And Substring(TXMVTO_CNTB.CO_CNTA_EM01,1, ' + @INNU_DGTO + ') In(SELECT SUBSTRING(Convert(Varchar(20), #TTCLAS_0005.CO_CNTA_EMPR), 1, ' + @INNU_DGTO + ')              
               FROM #TTCLAS_0005                
               Where #TTCLAS_0005.NU_COLU IS NOT NULL AND #TTCLAS_0005.NU_CONT = ' + @VNNU_CONT_0001 + '              
               )              
          And TMPLAN_EMPR.CO_CLAS_CNTA = "' + @CSCO_CLAS_CNTA + '"               
         Group By TXMVTO_CNTB.CO_UNID_CNTB, TXMVTO_CNTB.CO_OPRC_CNTB, TXMVTO_CNTB.NU_ASTO_0001, TXMVTO_CNTB.NU_SECU,Substring(TXMVTO_CNTB.CO_CNTA_EM01,1, ' + @INNU_DGTO + '), TXMVTO_CNTB.NU_MESE ')              
             
            
            
             
       IF (@@ERROR != 0)                
       Begin      
        Close CU_CLAS_CNTA_Q01_4                
        Deallocate CU_CLAS_CNTA_Q01_4                
        Return                
       End               
              
-- Inserta saldos de cada cuenta por mes                     
--------------------------------------------------------------------------------------------                      
             
-- Inserta saldos de cada cuenta por mes                     
          
--------------------------------------------------------------------------------------------                      
          
    Exec( 'INSERT Into #TTCLAS_8888 (MONTO, NU_ASTO, NU_SEGU, NU_ANNO, NU_MESE, NU_SECU_QUIE)                         
     Select (SUM(TASALD_EMPR.IM_CARG_CNTB) - SUM(TASALD_EMPR.IM_ABON_CNTB)), TASALD_EMPR.CO_CNTA_EMPR,                          
      MAX(TTCLAS_CNTA.NU_SECU_CLAS),TASALD_EMPR.NU_ANNO, TASALD_EMPR.NU_MESE, ' + @VNNU_CONT_0001 + '                         
     From  TASALD_EMPR                         
       Left Outer Join TMPLAN_EMPR                          
       On TMPLAN_EMPR.CO_EMPR = TASALD_EMPR.CO_EMPR                          
       And  TMPLAN_EMPR.CO_CNTA_EMPR = TASALD_EMPR.CO_CNTA_EMPR                          
       Left Outer Join TTCLAS_CNTA                          
       On TTCLAS_CNTA.CO_EMPR = TMPLAN_EMPR.CO_EMPR                          
       And  TTCLAS_CNTA.CO_CLAS_CNTA = TMPLAN_EMPR.CO_CLAS_CNTA                          
       Left Outer Join TMUNID_CNTB                    
       On TMUNID_CNTB.CO_EMPR = TASALD_EMPR.CO_EMPR                          
       And  TMUNID_CNTB.CO_UNID_CNTB = TASALD_EMPR.CO_UNID_CNTB                          
    Where TASALD_EMPR.CO_EMPR = "' + @ISCO_EMPR + '"                              
   AND TASALD_EMPR.NU_ANNO = ' + @INNU_ANNO +                      
   'AND TASALD_EMPR.NU_MESE <= ' + @INNU_MESE_0002 + '                    
      AND   TASALD_EMPR.NU_CNTB_EMPR = ' + @INNU_CNTB_EMPR + ' '+ @ISCA_WHER + '                              
      And Len(TASALD_EMPR.CO_CNTA_EMPR) = ' + @INNU_DGTO + '                     
      and TASALD_EMPR.TI_AUXI_EMPR IS NULL                
      AND TASALD_EMPR.CO_AUXI_EMPR IS NULL                
      AND TASALD_EMPR.CO_ORDE_SERV IS NULL                
      And TASALD_EMPR.CO_CNTA_EMPR In(Select CO_CNTA_EMPR From TMPLAN_EMPR t1, TTCLAS_CNTA t2                            
          Where t1.CO_EMPR = t2.CO_EMPR                            
          And t1.CO_CLAS_CNTA = t2.CO_CLAS_CNTA )                            
--       And TASALD_EMPR.CO_CNTA_EMPR In(SELECT #TTCLAS_0005.CO_CNTA_EMPR                          
--        FROM #TTCLAS_0005                              
--       Where #TTCLAS_0005.NU_COLU IS NOT NULL AND #TTCLAS_0005.NU_CONT = ' + @VNNU_CONT_0001 + ' )                    
   And TMPLAN_EMPR.CO_CLAS_CNTA = "' + @CSCO_CLAS_CNTA + '"                      
     Group By TASALD_EMPR.CO_UNID_CNTB, TASALD_EMPR.CO_CNTA_EMPR , TASALD_EMPR.NU_MESE, TASALD_EMPR.NU_ANNO ')                          
                
                
               
      End  -- Fin If @VNNU_CONT > 9              
    
       
              
     If @VNNU_CONT > 0 AND @VNNU_CONT <= 9--18              
      Begin              
                     
       Exec( 'UPDATE #TTCLAS_0004 SET [COLUMNA_' + @VNNU_CONT + '] = Convert(Int, ' + @VSCO_CNTA_EMPR + ') ' +               
       ' WHERE NU_CONT = ' + @VNNU_SEGU)              
      
        
       
      End        
    End        
    
                    
        
If @VNNU_CONT != 0            
 Begin            
  Exec( 'UPDATE #TTCLAS_0004 SET [COLUMNA_' + @VNNU_CONT + '] = t1.IM_MVTO_CNTB '+            
  ' FROM #TTCLAS_0001 t1 INNER JOIN #TTCLAS_0004 t2 ON t1.NU_ASTO=t2.NU_ASTO AND t1.NU_SECU=t2.NU_SECU          
   AND t1.NU_MESE = t2.NU_MESE ' +            
  'WHERE T1.CO_CLAS_CNTA = "' + @CSCO_CLAS_CNTA + '"             
  AND t1.NU_MESE = "' +  @CNNU_MESE  + '"           
  AND t2.NU_CONT IS NULL           
  AND t1.CO_CNTA_EMPR= t2.CO_CNTA_EMPR           
  AND t1.NU_ASTO = ' + @CSNU_ASTO + '            
  AND t1.NU_SECU = ' + @CNNU_SECU_0001 + '              
  AND t1.CO_CNTA_EMPR=''' + @CSCO_CNTA_EMPR + '''' )            
          
    
Exec(                
  'INSERT INTO #TTCLAS_0004 (DE_VIEN,CO_CNTA_EMPR,COLUMNA_' + @VNNU_CONT + ' , NU_MESE,NU_SECU_QUIE)                      
    SELECT "VIENEN", NU_ASTO,                     
   CASE                       
   WHEN NU_MESE = 0 THEN  0.00                      
   WHEN NU_MESE = 1 THEN                
   (SELECT SUM(MONTO) FROM #TTCLAS_8888                      
     WHERE                       
     NU_ASTO= ' + @VSCO_CNTA_EMPR + 'AND                      
     NU_MESE < 1                      
     GROUP BY   NU_ASTO  )                      
              
  WHEN NU_MESE = 2 THEN (SELECT SUM(MONTO) FROM #TTCLAS_8888                      
   WHERE                       
   NU_ASTO= ' + @VSCO_CNTA_EMPR + 'AND                      
     NU_MESE < 2                      
         GROUP BY NU_ASTO  )                      
  WHEN NU_MESE = 3 THEN (SELECT SUM(MONTO) FROM #TTCLAS_8888                      
   WHERE                       
    NU_ASTO= ' + @VSCO_CNTA_EMPR + 'AND                      
     NU_MESE <3                      
         GROUP BY NU_ASTO  )                      
  WHEN NU_MESE = 4 THEN (SELECT SUM(MONTO) FROM #TTCLAS_8888                      
   WHERE                       
    NU_ASTO= ' + @VSCO_CNTA_EMPR + 'AND                      
     NU_MESE <4                      
  GROUP BY NU_ASTO  )                      
  WHEN NU_MESE = 5 THEN (SELECT SUM(MONTO) FROM #TTCLAS_8888                      
   WHERE                       
    NU_ASTO= ' + @VSCO_CNTA_EMPR + 'AND                      
     NU_MESE <5                      
         GROUP BY NU_ASTO  )                                          
  WHEN NU_MESE = 6 THEN (SELECT SUM(MONTO) FROM #TTCLAS_8888                      
     WHERE                       
     NU_ASTO= ' + @VSCO_CNTA_EMPR + 'AND           
     NU_MESE <6                      
         GROUP BY NU_ASTO  )                      
  WHEN NU_MESE = 7 THEN (SELECT SUM(MONTO) FROM #TTCLAS_8888                      
   WHERE                       
   NU_ASTO= ' + @VSCO_CNTA_EMPR + 'AND                      
   NU_MESE <7                      
         GROUP BY NU_ASTO  )                      
  WHEN NU_MESE = 8 THEN (SELECT SUM(MONTO) FROM #TTCLAS_8888                      
    WHERE                       
    NU_ASTO= ' + @VSCO_CNTA_EMPR + 'AND                      
     NU_MESE <8                      
         GROUP BY NU_ASTO  )                      
  WHEN NU_MESE = 9 THEN (SELECT SUM(MONTO) FROM #TTCLAS_8888                      
    WHERE                       
     NU_ASTO= ' + @VSCO_CNTA_EMPR + 'AND                      
     NU_MESE <9                      
         GROUP BY NU_ASTO  )                      
  WHEN NU_MESE = 10 THEN (SELECT SUM(MONTO) FROM #TTCLAS_8888                      
   WHERE                       
   NU_ASTO= ' + @VSCO_CNTA_EMPR + 'AND                      
    NU_MESE <10                      
         GROUP BY NU_ASTO  )                      
  WHEN NU_MESE = 11 THEN (SELECT SUM(MONTO) FROM #TTCLAS_8888                      
   WHERE                       
   NU_ASTO= ' + @VSCO_CNTA_EMPR + 'AND                      
     NU_MESE <11                      
         GROUP BY NU_ASTO  )                      
  WHEN NU_MESE = 12 THEN (SELECT SUM(MONTO) FROM #TTCLAS_8888                      
     WHERE                       
     NU_ASTO= ' + @VSCO_CNTA_EMPR + 'AND                      
     NU_MESE <12                      
         GROUP BY NU_ASTO  ) 
  WHEN NU_MESE = 13 THEN (SELECT SUM(MONTO) FROM #ttclas_8888                  
     WHERE                   
     NU_ASTO= ' + @VSCO_CNTA_EMPR + ' AND                  
     NU_MESE <13                  
     GROUP BY NU_ASTO  )                       
  END,NU_MESE,NU_SECU_QUIE                    
  FROM #TTCLAS_8888                      
  WHERE NU_ASTO= ' + @VSCO_CNTA_EMPR + '               
  AND NU_MESE > = ' + @INNU_MESE_0001 + '                 
  AND NU_MESE < = ' + @INNU_MESE_0002 + '                    
     AND NOT EXISTS (              
  SELECT * FROM #TTCLAS_0004              
  WHERE #TTCLAS_0004.CO_CNTA_EMPR = #TTCLAS_8888.NU_ASTO              
  AND #TTCLAS_0004.NU_MESE = #TTCLAS_8888.NU_MESE              
  AND #TTCLAS_0004.DE_CLAS_CNTA IS NULL              
  AND #TTCLAS_0004.DE_VIEN = ''VIENEN''              
  AND #TTCLAS_0004.NU_SECU_QUIE = #TTCLAS_8888.NU_SECU_QUIE              
  )       
 GROUP BY NU_ASTO , NU_MESE,NU_SECU_QUIE')     
  
  
Exec(       
  
'INSERT INTO #TTCLAS_0004 (DE_CLAS_CNTA,NU_SECU,NU_ASTO, NU_CONT , COLUMNA_' + @VNNU_CONT + ' , NU_MESE,NU_SECU_QUIE)                        
          
SELECT DESCRIPCION , NULL,NULL,1,          
          
      CASE                         
          
    WHEN NU_MESE = 0 THEN ' + @VSCO_CNTA_EMPR +                         
   'WHEN NU_MESE = 1 THEN ' + @VSCO_CNTA_EMPR +                       
   'WHEN NU_MESE = 2 THEN ' + @VSCO_CNTA_EMPR +                        
   'WHEN NU_MESE = 3  THEN ' + @VSCO_CNTA_EMPR +                 
   'WHEN NU_MESE = 4  THEN ' + @VSCO_CNTA_EMPR +            
   'WHEN NU_MESE = 5  THEN ' + @VSCO_CNTA_EMPR +                                            
   'WHEN NU_MESE = 6  THEN ' + @VSCO_CNTA_EMPR +                        
   'WHEN NU_MESE = 7  THEN ' + @VSCO_CNTA_EMPR +                         
   'WHEN NU_MESE = 8  THEN ' + @VSCO_CNTA_EMPR +                         
   'WHEN NU_MESE = 9  THEN ' + @VSCO_CNTA_EMPR +                         
   'WHEN NU_MESE = 10 THEN ' + @VSCO_CNTA_EMPR +                       
   'WHEN NU_MESE = 11  THEN ' + @VSCO_CNTA_EMPR +                        
   'WHEN NU_MESE = 12    THEN ' + @VSCO_CNTA_EMPR +                        
   ' END ,           
      NU_MESE , NU_SECU_QUIE    FROM  #TTCLAS_8888                        
  WHERE NU_ASTO = ' + @VSCO_CNTA_EMPR + '                 
  AND NU_MESE > = ' + @INNU_MESE_0001 + '                   
  AND NU_MESE < = ' + @INNU_MESE_0002 + '            
  AND  NOT EXISTS (                
  SELECT * FROM #TTCLAS_0004                
                 
  )              
  GROUP BY  NU_MESE,NU_SECU_QUIE,MONTO,DESCRIPCION')      
      
  
  
        
  
 End            
   
              
    Fetch CU_CLAS_CNTA_Q01_4 Into @CSCO_CNTA_EMPR, @CSNU_ASTO, @CSCO_CLAS_CNTA, @CSDE_CLAS_CNTA, @CSNU_SECU_CLAS, @CNNU_MESE,            
@CNNU_SECU_0001                
              
 Select @VNNU_FETC_0001 = @@FETCH_STATUS              
              
  END                
           
            
             
Close CU_CLAS_CNTA_Q01_4                
Deallocate CU_CLAS_CNTA_Q01_4              
              
            
              
         
If @ISTI_PRES = 'REP'                  
 Begin                 
  If @ISTI_ORDE = 'VOU'              
   Begin                     
 Select DE_VIEN,IsNull(Substring(NU_ASTO, 1, 3), @ISCO_UNID_CNTB), Substring(NU_ASTO, 4, 3), Substring(NU_ASTO, 7, 10), FE_ASTO_CNTB, DE_GLOS,              
 IsNull(NU_CONT,0), NU_SECU_QUIE,               
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_1) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1/@INNV_PRES,2)),0)))             
 Else 0 End)Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1,2)),0)) End,            
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_1) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1,2)),0)) End,            
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_2) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2,2)),0)) End,              
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_2) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2/@INNV_PRES,2)),0))) Else 0 End)            
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2,2)),0)) End,             
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_3) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3,2)),0)) End,              
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_3) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3/@INNV_PRES,2)),0))) Else 0 End)         
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3,2)),0)) End,            
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_4) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4,2)),0)) End,              
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_4) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4,2)),0)) End,            
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_5) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5,2)),0)) End,              
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_5) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5,2)),0)) End,            
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_6) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6,2)),0)) End,              
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_6) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6,2)),0)) End,            
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_7) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7,2)),0)) End,              
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_7) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7,2)),0)) End,            
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_8) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8,2)),0)) End,              
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_8) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8,2)),0)) End,            
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_9) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9,2)),0)) End,              
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_9) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9,2)),0)) End,            
 DE_CLAS_CNTA,             
 NU_MESE,@INNU_DGTO,Convert(Varchar,NU_MESE)+Convert(Varchar,NU_SECU_QUIE)              
 From #TTCLAS_0004--, #TTCLAS_0007              
    GROUP BY Substring(NU_ASTO, 1, 3), NU_ASTO, NU_SECU,FE_ASTO_CNTB, DE_GLOS, NU_CONT, DE_CLAS_CNTA, NU_SECU_QUIE, NU_MESE, DE_VIEN                
        
    order by NU_MESE,  NU_SECU_QUIE,3   , DE_VIEN          
End              
              
  If @ISTI_ORDE = 'OPE'              
   Begin                     
 Select DE_VIEN,IsNull(Substring(NU_ASTO, 1, 3), @ISCO_UNID_CNTB), Substring(NU_ASTO, 4, 3), Substring(NU_ASTO, 7, 10), FE_ASTO_CNTB, DE_GLOS,               
 IsNull(NU_CONT,0), NU_SECU_QUIE,               
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_1) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1/@INNV_PRES,2)),0))) Else 0 End)            
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1,2)),0)) End,            
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_1) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1/@INNV_PRES,2)),0))) Else 0 End)            
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1,2)),0)) End,            
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_2) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2,2)),0)) End,              
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_2) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2/@INNV_PRES,2)),0))) Else 0 End)            
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2,2)),0)) End,             
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_3) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3,2)),0)) End,              
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_3) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3,2)),0)) End,            
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_4) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4,2)),0)) End,              
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_4) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4,2)),0)) End,            
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_5) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5,2)),0)) End,              
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_5) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5,2)),0)) End,            
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_6) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6,2)),0)) End,              
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_6) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6,2)),0)) End,            
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_7) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7,2)),0)) End,           
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_7) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7,2)),0)) End,            
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_8) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8,2)),0)) End,              
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_8) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8,2)),0)) End,            
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_9) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9,2)),0)) End,              
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_9) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9,2)),0)) End,            
 DE_CLAS_CNTA,             
 NU_MESE,@INNU_DGTO,Convert(Varchar,NU_MESE)+Convert(Varchar,NU_SECU_QUIE)              
 From #TTCLAS_0004--, #TTCLAS_0007              
 GROUP BY NU_ASTO, NU_SECU,NU_SECU,FE_ASTO_CNTB, DE_GLOS, NU_CONT, DE_CLAS_CNTA, NU_SECU_QUIE, NU_MESE   , DE_VIEN               
 order by  NU_MESE,NU_SECU_QUIE  ,3,DE_VIEN            
 End              
              
  If @ISTI_ORDE = 'FEC'              
   Begin                     
  Select DE_VIEN,IsNull(Substring(NU_ASTO, 1, 3), @ISCO_UNID_CNTB), Substring(NU_ASTO, 4, 3), Substring(NU_ASTO, 7, 10), FE_ASTO_CNTB, DE_GLOS,               
  IsNull(NU_CONT,0), NU_SECU_QUIE,               
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_1) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1/@INNV_PRES,2)),0))) Else 0 End)            
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_1) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_2) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_2) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2/@INNV_PRES,2)),0))) Else 0 End)            
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2,2)),0)) End,             
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_3) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_3) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_4) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_4) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_5) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_5) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_6) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_6) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6/@INNV_PRES,2)),0))) Else 0 End)            
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_7) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7,2)),0)) End,              
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_7) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7,2)),0)) End,            
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_8) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8,2)),0)) End,              
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_8) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8,2)),0)) End,            
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_9) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9,2)),0)) End,              
 Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_9) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9/@INNV_PRES,2)),0))) Else 0 End)             
 Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9,2)),0)) End,            
 DE_CLAS_CNTA, NU_MESE,@INNU_DGTO,Convert(Varchar,NU_MESE)+Convert(Varchar,NU_SECU_QUIE)                 
 From #TTCLAS_0004            
 GROUP BY NU_ASTO,NU_SECU, FE_ASTO_CNTB, DE_GLOS, NU_CONT, DE_CLAS_CNTA, NU_SECU_QUIE, NU_MESE   , DE_VIEN             
 order by NU_MESE, NU_SECU_QUIE, FE_ASTO_CNTB ,  3, DE_VIEN             
  End              
              
 End              
              
If @ISTI_PRES = 'TAB'                    
   Begin                      
  If @ISTI_ORDE = 'VOU'              
   Begin                   
  Select IsNull(Substring(NU_ASTO, 1, 3), @ISCO_UNID_CNTB), Substring(NU_ASTO, 4, 3), Substring(NU_ASTO, 7, 10), FE_ASTO_CNTB, DE_GLOS,                
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_1) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1/@INNV_PRES,2)),0)))             
  Else 0 End)Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_1) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1/@INNV_PRES,2)),0)))             
  Else 0 End) Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_2) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2/@INNV_PRES,2)),0)))             
  Else 0 End) Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_2) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2/@INNV_PRES,2)),0)))             
  Else 0 End)Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2,2)),0)) End,             
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_3) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3/@INNV_PRES,2)),0)))             
  Else 0 End) Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3,2)),0)) End,              Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_3) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3/@INNV_PRES
  
,2)),0)))             
  Else 0 End) Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_4) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4/@INNV_PRES,2)),0)))             
  Else 0 End) Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_4) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4/@INNV_PRES,2)),0)))             
  Else 0 End) Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_5) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5/@INNV_PRES,2)),0)))             
  Else 0 End) Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_5) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5/@INNV_PRES,2)),0)))             
  Else 0 End) Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_6) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6/@INNV_PRES,2)),0)))             
  Else 0 End) Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_6) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6/@INNV_PRES,2)),0)))             
  Else 0 End) Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_7) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7/@INNV_PRES,2)),0)))             
  Else 0 End) Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_7) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7/@INNV_PRES,2)),0)))             
  Else 0 End) Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_8) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8/@INNV_PRES,2)),0)))             
  Else 0 End) Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_8) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8/@INNV_PRES,2)),0)))            
  Else 0 End) Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_9) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9/@INNV_PRES,2)),0)))             
  Else 0 End) Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_9) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9/@INNV_PRES,2)),0)))             
  Else 0 End) Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9,2)),0)) End,            
  DE_CLAS_CNTA, NU_SECU_QUIE, NU_MESE  ,Convert(Varchar,NU_MESE)+Convert(Varchar,NU_SECU_QUIE)                 
  From #TTCLAS_0004                  
  GROUP BY NU_ASTO,NU_SECU, FE_ASTO_CNTB, DE_GLOS, NU_CONT, DE_CLAS_CNTA, NU_SECU_QUIE, NU_MESE  , DE_VIEN             
  order by NU_MESE, NU_SECU_QUIE, 3  , DE_VIEN             
 End              
              
  If @ISTI_ORDE = 'OPE'              
   Begin                   
  Select IsNull(Substring(NU_ASTO, 1, 3), @ISCO_UNID_CNTB), Substring(NU_ASTO, 4, 3), Substring(NU_ASTO, 7, 10), FE_ASTO_CNTB, DE_GLOS,                
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_1) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1/@INNV_PRES,2)),0)))             
  Else 0 End)Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_1) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_2) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_2) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2/@INNV_PRES,2)),0))) Else 0 End)            
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2,2)),0)) End,             
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_3) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_3) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_4) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_4) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_5) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_5) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_6) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_6) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_7) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_7) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_8) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_8) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_9) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_9) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9,2)),0)) End,            
  DE_CLAS_CNTA, NU_SECU_QUIE, NU_MESE,Convert(Varchar,NU_MESE)+Convert(Varchar,NU_SECU_QUIE)                   
  From #TTCLAS_0004                  
  GROUP BY NU_ASTO, NU_SECU,FE_ASTO_CNTB, DE_GLOS, NU_CONT, DE_CLAS_CNTA, NU_SECU_QUIE, NU_MESE  , DE_VIEN             
  order by  NU_MESE,NU_SECU_QUIE,2  , DE_VIEN             
  End              
              
  If @ISTI_ORDE = 'FEC'              
   Begin                   
  Select IsNull(Substring(NU_ASTO, 1, 3), @ISCO_UNID_CNTB), Substring(NU_ASTO, 4, 3), Substring(NU_ASTO, 7, 10), FE_ASTO_CNTB, DE_GLOS,                
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_1) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1/@INNV_PRES,2)),0))) Else 0 End)            
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_1) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_1,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_2) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_2) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2/@INNV_PRES,2)),0))) Else 0 End)            
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_2,2)),0)) End,             
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_3) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3/@INNV_PRES,2)),0))) Else 0 End)       Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_3) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_3,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_4) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_4) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_4,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_5) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_5) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_5,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_6) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_6) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_6,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_7) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_7) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_7,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_8) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_8) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_8,2)),0)) End,            
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_9) >= 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9,2)),0)) End,              
  Case IsNull(NU_CONT, 0) When 0 Then (Case When Sum(COLUMNA_9) < 0 Then Abs(Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9/@INNV_PRES,2)),0))) Else 0 End)             
  Else Sum(IsNull(Convert(Numeric(16,4),Round(#TTCLAS_0004.COLUMNA_9,2)),0)) End,            
  DE_CLAS_CNTA, NU_SECU_QUIE, NU_MESE,Convert(Varchar,NU_MESE)+Convert(Varchar,NU_SECU_QUIE)                   
  From #TTCLAS_0004                  
  GROUP BY NU_ASTO,NU_SECU, FE_ASTO_CNTB, DE_GLOS, NU_CONT, DE_CLAS_CNTA, NU_SECU_QUIE, NU_MESE  , DE_VIEN             
  order by NU_MESE, NU_SECU_QUIE, Convert(DateTime, FE_ASTO_CNTB, 103)    , DE_VIEN             
 End              
              
   End                
              
              
/*------------------------------------- Fin --------------------------------------------------------*/ 
GO
